<?php
require_once 'BaseController.php';

/**
 * 【SP版】店舗一覧 コントローラ
 */
class Mobileapi_ShoplistController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $cat = $params['Category'];

        if ($cat == 'area')
        {
            $code = $this->convCityCode($params['Code']);
            $this->makeTemplateParams($params, $cat, '', $code);
            $this->displayBody2('result_area');
        }
        else if ($cat == 'area2')
        {
            $codes = array();
            foreach ($params['CityCd'] as $code)
            {
                $codes[] = $this->convCityCode($code);
            }
            $this->makeTemplateParams($params, $cat, '', $codes);
            $this->displayBody2('result_area');
        }
        else if ($cat == 'line')
        {
            $codes = array();
            $type = '';

            // tho 追加
            if (!empty($params['RailCd']))
            {
                foreach ($params['RailCd'] as $code)
                {
                    $codes[] = $this->convRailCode($code);
                }
                $type = 'RailCd';
            }

            // tho 追加
            else if (!empty($params['StCd']))
            {
                foreach ($params['StCd'] as $code)
                {
                    $codes[] = $this->convStationCode($code);
                }
                $type = 'StCd';
            }

            $this->makeTemplateParams($params, $cat, $type, $codes);
            $this->displayBody2('result_line');
        }
    }

    private function convCityCode($code)
    {
        return substr($code, 0, 2) . '-' . $code;
    }

    private function convRailCode($code)
    {
        return substr($code, 0, 2) . '-' . substr($code, 2, 4);
    }

    private function convStationCode($code)
    {
        return substr($code, 0, 2) . '-' . substr($code, 2, 7);
    }

    private function getPrefNameByPrefCd($params)
    {
        $model1 = new Search_Model_Selareasearch();
        $data = $model1->getPrefNameByPrefCd($params);
        foreach ($data as $k => $v)
        {
            $data[$k]['PrefName'] = $this->getPrefName($v['M01_TODOUFUKEN_CD']);
        }
        return $data;
    }

    private function makeTemplateParams($params, $cat, $type, $code)
    {
        // $params = array();
        if ($cat == 'area' || $cat == 'area2')
        {
            $params['SrhKbn'] = '01';

            $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'CityCd');

            $model = new Shop_Model_ListForCity();
            // $model_seo = new Shop_Model_SeoParamForCity();

            $this->_data['Mapping_pref'] = $this->getPrefNameByPrefCd($params);

            $model2 = new Search_Model_Selcyo();
            $this->_data['Mapping_city'] = $model2->getShikucyosonNameByShikucyosonCd($params);
            $this->_data['Mapping_pref_city_cd'] = $params['CityCd'][0];
        }
        elseif ($cat == 'line')
        {
            $params['SrhKbn'] = '02';

            $params['PrefCd'][] = "13";

            // tho add
            if ($type == 'StCd')
            {
                $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'StCd');
            }
            else
            {
                $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'RailCd');
            }

            $model = new Shop_Model_ListForRail();
            // $model_seo = new Shop_Model_SeoParamForRail();

            $this->_data['Mapping_pref'] = $this->getPrefNameByPrefCd($params);

            // $model2 = new Search_Model_Selcyo();
            // $this->_data['Mapping_city'] = $model2->getShikucyosonNameByShikucyosonCd($params);

            if ($type == 'RailCd')
            {
                $model2 = new Search_Model_Selensen();
                $this->_data['Mapping_ensen'] = $model2->getRailNameByRailCd($params);
            }
            if ($type == 'StCd')
            {
                $model3 = new Search_Model_Seleki();
                $this->_data['Mapping_eki'] = $model3->getStNameByStCd($params);
            }
        }

        $data = $model->getList($params);
        // $data = $model->convertList($data,'','',$params);

        // $seo_params = $model_seo->getList($params);

        $baseurl = $this->_config['apm']['baseurl'] . 'shop/shopdtl';
        $tenpoimageurl = $this->_config['apm']['apl']['imagesvurl'] . 'fs/tenpoimg/';

        $data = $model->convertList($data, $tenpoimageurl, $url, $params);

        // CALLNOTE対応暫定固定で記述
        foreach ($data as $key => $ekiLst)
        {
            foreach ($ekiLst as $key1 => $tenpo)
            {
                if (!empty($tenpo['CONNECTION_NO']))
                {
                    $ekiLst[$key1]['CONNECTION_NO'] = substr($tenpo['CONNECTION_NO'], 0, 4) . '12';
                }
            }
        }
        // Callクレヨン対応
        $smodel = new Shop_Model_Detail();
        if ($cat == 'area' || $cat == 'area2')
        {
            foreach ($data as $key => $tenpo)
            {
                $callCrayonNumber = $smodel->getCallCrayonNumber($this->_config['callcrayons']['api'], $this->_config['callcrayons']['service_id'], $tenpo['M05_TENPO_CD'], '36');
                $data[$key]['CALL_CRAYON_NO'] = $callCrayonNumber;
                /*
                 * if (! empty ( $data [$key] ['CALL_CRAYON_NO'] )) { // フリー番号ではない・・・ $data [$key]
                 * ['hasFreeTel'] = false; $data [$key] ['IS_FREE_CALL'] = false; }
                 */
            }
        }
        elseif ($cat == 'line')
        {
            foreach ($data as $key => $ekiLst)
            {
                foreach ($ekiLst['shopList'] as $key1 => $tenpo)
                {
                    $callCrayonNumber = $smodel->getCallCrayonNumber($this->_config['callcrayons']['api'], $this->_config['callcrayons']['service_id'], $tenpo['M05_TENPO_CD'], '36');
                    $data[$key]['shopList'][$key1]['CALL_CRAYON_NO'] = $callCrayonNumber;
                    // $ekiLst ['shopList'] [$key1] ['CALL_CRAYON_NO'] = $callCrayonNumber;
                }
            }
        }

        $this->_data['Mapping'] = $data;
        $this->_data['Mapping_tenpoimageurl'] = $tenpoimageurl;

        $meta_data = Apm_Bukkenurl::setCommonMeta($params['SrhKbn'], $params, $this->_data['research_data'], true);

        if ($cat == 'area' || $cat == 'area2')
        {
            $this->_data['Mapping_area_name'] = $meta_data['AREA_FULL'];
            $this->_data['Mapping_area_name_other'] = $meta_data['AREA_FULL_OTHER'];
        }
        else if ($cat == 'line')
        {
            $this->_data['Mapping_area_name'] = $meta_data['STATION_CAT2'];
        }
        $this->_data['Mapping_cat'] = $cat;
        $this->_data['RandomPrefs'] = $this->getRandomPrefs(5);
    }

}
?>